#include "Button.h"
#include "AnimationSprite.h"
#include "Vector.h"
#include "Animation.h"
#include "GameUtils.h"

Button::Button(int x, int y, int width, int height):MoveableObj(x, y, width, height, 0)
{
	isTouchDown = false;
	aniSpriteTouchUp = 0;
	aniSpriteTouchDown = 0;
	aniSpriteBackground = 0;
}

Button::~Button()
{}

void Button::SetImageForTouchUp(AnimationSprite *aniSprite, int aniID, int spriteIndex)
{
	aniSpriteTouchUp = aniSprite;
	aniIDTouchUp = aniID;
	spriteIndexTouchUp = spriteIndex;
	TouchUp();
}
void Button::SetImageForTouchDown(AnimationSprite *aniSprite, int aniID, int spriteIndex)
{
	aniSpriteTouchDown = aniSprite;
	aniIDTouchDown = aniID;
	spriteIndexTouchDown = spriteIndex;
}

void Button::SetImageForBackground(AnimationSprite *aniSprite, int aniID, int spriteIndex)
{
	aniSpriteBackground = aniSprite;
	aniIDBackground = aniID;
	spriteIndexBackground = spriteIndex;
}

bool Button::IsTouchDownBy(int x, int y)
{
	if(IsPointInRect(x, y, rect) && isTouchDown == false)
		return true;
	else return false;
}

bool Button::IsTouchUpBy(int x, int y)
{
	if(IsPointInRect(x, y, rect) && isTouchDown == true)
		return true;
	else return false;
}

void Button::TouchUp()
{
	isTouchDown =false;
}

void Button::TouchDown()
{
	isTouchDown = true;
}

void Button::Render()
{
	if(isTouchDown == true){
		if(aniSpriteTouchDown != 0)
		{
			aniSpriteTouchDown->SetCurAnimation(aniIDTouchDown)->SetSpriteIndex(true, spriteIndexTouchDown);
			if(aniSpriteTouchDown != 0)aniSpriteTouchDown->Render(pos->x, pos->y, 1.0f);
		}
	}
	else {
		if(aniSpriteTouchUp!= 0)
		{
			aniSpriteTouchUp->SetCurAnimation(aniIDTouchUp)->SetSpriteIndex(true, spriteIndexTouchUp);
			if(aniSpriteTouchUp != 0) aniSpriteTouchUp->Render(pos->x, pos->y, 1.0f);
		}
	}
	if(aniSpriteBackground != 0) 
	{
		aniSpriteBackground->SetCurAnimation(aniIDBackground)->SetSpriteIndex(true, spriteIndexBackground);
		aniSpriteBackground->Render(pos->x, pos->y, 1.0f);
	}
}

void Button::SetID(int id)
{
	this->id = id;
}